package com.xhwl.centiotdata.service.vehicleschedule;

import com.alibaba.fastjson.JSONObject;
import com.xhwl.centiotdata.integration.vehicleschedule.VehicleScheduleFactory;
import com.xhwl.centiotdata.integration.vehicleschedule.VehicleScheduleIntegrationConfig;
import com.xhwl.centiotdata.integration.vehicleschedule.VehicleScheduleSystemHandler;
import com.xhwl.centiotdata.integrator.vehicleschedule.sinoiov.TruckQueryResponseDTO;
import com.xhwl.centiotdata.integrator.vehicleschedule.station.StationDTO;
import com.xhwl.centiotdata.service.cent.ISpaceCentService;
import com.xhwl.common.dto.cent.iotdata.vehicleschedule.*;
import com.xhwl.common.pojo.ResultBuilder;
import com.xhwl.common.pojo.ResultJson;
import com.xhwl.common.query.cent.sdata.SpaceQuery;
import com.xhwl.common.utils.ChkUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.LinkedList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Falcon.cao @Copyright (C), 2022, 兴海物联
 * Date: 2022/10/5 16:50
 * Description: 车辆调度开放接口服务
 */
@Service
public class VehicleScheduleServiceImpl implements VehicleScheduleService {

    @Autowired
    private VehicleScheduleFactory vehicleScheduleFactory;

    @Autowired
    private ISpaceCentService spaceCentService;

    /**
     * 获取车辆调度集成配置
     */
    private VehicleScheduleIntegrationConfig onFindingConfig(Integer projectId) {
        List<VehicleScheduleIntegrationConfig> configs = vehicleScheduleFactory.onFindingIntegrationConfigs(projectId);
        return configs.get(0);
    }

    @Override
    public ResultJson<VehicleScheduleResponseDTO> onCheckingTruckEnter(VehicleScheduleRequestsDTO dto) {
        VehicleScheduleIntegrationConfig config = onFindingConfig(dto.getProjectId());
        VehicleScheduleSystemHandler handler = vehicleScheduleFactory.onFindingIntegrationHandler(config.getProductName());
        return ResultBuilder.success(handler.onCheckingTruckEntry(config, dto));
    }

    @Override
    public ResultJson<VehicleScheduleResponseDTO> onSyncingTruckEvents(VehicleScheduleRequestsDTO dto) {
        VehicleScheduleIntegrationConfig config = onFindingConfig(dto.getProjectId());
        VehicleScheduleSystemHandler handler = vehicleScheduleFactory.onFindingIntegrationHandler(config.getProductName());
        return ResultBuilder.success(handler.onSyncingTruckEvents(config, dto));
    }

    @Override
    public ResultJson<VehicleScheduleResponseDTO> onSyncingWeighResult(VehicleScheduleRequestsDTO dto) {
        VehicleScheduleIntegrationConfig config = onFindingConfig(dto.getProjectId());
        VehicleScheduleSystemHandler handler = vehicleScheduleFactory.onFindingIntegrationHandler(config.getProductName());
        return ResultBuilder.success(handler.onSyncingWeighResult(config, dto));
    }

    @Override
    public ResultJson<VehicleScheduleResponseDTO> onSyncingSpaceStatus(VehicleScheduleRequestsDTO dto) {
        VehicleScheduleIntegrationConfig config = onFindingConfig(dto.getProjectId());
        VehicleScheduleSystemHandler handler = vehicleScheduleFactory.onFindingIntegrationHandler(config.getProductName());
        return ResultBuilder.success(handler.onSyncingSpaceStatus(config, dto));
    }

    @Override
    public ResultJson<List<VehicleScheduleResponseDTO>> listTruckQueue(VehicleScheduleRequestsDTO dto) {
        List<VehicleScheduleIntegrationConfig> configs = vehicleScheduleFactory.onFindingIntegrationConfigs(dto.getProjectId());
        List<VehicleScheduleResponseDTO> targets = new LinkedList<>();
        for (VehicleScheduleIntegrationConfig config : configs) {
            VehicleScheduleSystemHandler handlers = vehicleScheduleFactory.onFindingIntegrationHandler(config.getProductName());
            List<VehicleScheduleResponseDTO> target = handlers != null ? handlers.onListingQueue(config, dto) : null;
            if (ChkUtil.isNotNull(target)) {
                targets.addAll(target);
            }
        }
        return ResultBuilder.success(targets);
    }

    @Override
    public ResultJson<List<VehicleScheduleStatisticsResponseDTO>> onQueryPositionTruckQueues(VehicleScheduleStatisticsRequestsDTO dto) {
        VehicleScheduleIntegrationConfig config = onFindingConfig(dto.getProjectId());
        VehicleScheduleSystemHandler handler = vehicleScheduleFactory.onFindingIntegrationHandler(config.getProductName());
        return ResultBuilder.success(handler.onQueryPositionTruckQueues(config, dto));
    }

    @Override
    public ResultJson<List<VehicleScheduleStatisticsResponseDTO>> onQueryPositionTruckDetail(VehicleScheduleStatisticsRequestsDTO dto) {
        VehicleScheduleIntegrationConfig config = onFindingConfig(dto.getProjectId());
        VehicleScheduleSystemHandler handler = vehicleScheduleFactory.onFindingIntegrationHandler(config.getProductName());
        return ResultBuilder.success(handler.onQueryPositionTruckDetail(config, dto));
    }

    @Override
    public ResultJson<List<VehicleScheduleStatisticsResponseDTO>> onQueryPositionTruckCounts(VehicleScheduleStatisticsRequestsDTO dto) {
        VehicleScheduleIntegrationConfig config = onFindingConfig(dto.getProjectId());
        VehicleScheduleSystemHandler handler = vehicleScheduleFactory.onFindingIntegrationHandler(config.getProductName());
        return ResultBuilder.success(handler.onQueryPositionTruckCounts(config, dto));
    }

    @Override
    public ResultJson<List<VehicleScheduleStatisticsResponseDTO>> onQueryPositionTruck(VehicleScheduleStatisticsRequestsDTO dto) {
        VehicleScheduleIntegrationConfig config = onFindingConfig(dto.getProjectId());
        VehicleScheduleSystemHandler handler = vehicleScheduleFactory.onFindingIntegrationHandler(config.getProductName());
        return ResultBuilder.success(handler.onQueryPositionTruck(config, dto));
    }

    @Override
    public ResultJson<List<VehicleScheduleStatisticsResponseV2DTO>> onQueryOrderStatistics(VehicleScheduleStatisticsRequestsDTO dto) {
        VehicleScheduleIntegrationConfig config = onFindingConfig(dto.getProjectId());
        VehicleScheduleSystemHandler handler = vehicleScheduleFactory.onFindingIntegrationHandler(config.getProductName());
        return ResultBuilder.success(handler.onQueryOrderStatistics(config, dto));
    }

    @Override
    public ResultJson<List<VehicleScheduleOrderFlowStateResponseDTO>> onQueryFlowsStatistics(VehicleScheduleStatisticsRequestsDTO dto) {
        VehicleScheduleIntegrationConfig config = onFindingConfig(dto.getProjectId());
        VehicleScheduleSystemHandler handler = vehicleScheduleFactory.onFindingIntegrationHandler(config.getProductName());
        return ResultBuilder.success(handler.onQueryFlowsStatistics(config, dto));
    }

    @Override
    public ResultJson<List<VehicleScheduleResponseV2DTO>> onQueryOrderFlowState(VehicleScheduleStatisticsRequestsDTO dto){
        VehicleScheduleIntegrationConfig config = onFindingConfig(dto.getProjectId());
        VehicleScheduleSystemHandler handler = vehicleScheduleFactory.onFindingIntegrationHandler(config.getProductName());
        return ResultBuilder.success(handler.onQueryOrderFlowState(config, dto));
    }

    @Override
    public ResultJson<List<VehicleScheduleWaitTestCallTruckDTO>> onQueryWaitTestCallTruckList(VehicleScheduleStatisticsRequestsDTO dto) throws Exception {
        VehicleScheduleIntegrationConfig config = onFindingConfig(dto.getProjectId());
        VehicleScheduleSystemHandler handler = vehicleScheduleFactory.onFindingIntegrationHandler(config.getProductName());
        return ResultBuilder.success(handler.onQueryWaitTestCallTruckList(config, dto));
    }

    @Override
    public ResultJson<List<VehicleSchedulePoundQueueInfoDTO>> onQueryPoundQueueInfo(VehicleScheduleStatisticsRequestsDTO dto) throws Exception {
        VehicleScheduleIntegrationConfig config = onFindingConfig(dto.getProjectId());
        VehicleScheduleSystemHandler handler = vehicleScheduleFactory.onFindingIntegrationHandler(config.getProductName());
        return ResultBuilder.success(handler.onQueryPoundQueueInfo(config, dto));
    }

    @Override
    public ResultJson<List<StationDTO>> stationQuery(VehicleScheduleStatisticsRequestsDTO dto) {
        VehicleScheduleIntegrationConfig config = onFindingConfig(dto.getProjectId());
        VehicleScheduleSystemHandler handler = vehicleScheduleFactory.onFindingIntegrationHandler(config.getProductName());
        return ResultBuilder.success(handler.stationQuery(config, dto));
    }

    @Override
    public ResultJson<List<TruckQueryResponseDTO>> truckQuery(VehicleScheduleStatisticsRequestsDTO dto) {
        VehicleScheduleIntegrationConfig config = onFindingConfig(dto.getProjectId());
        VehicleScheduleSystemHandler handler = vehicleScheduleFactory.onFindingIntegrationHandler(config.getProductName());
        return ResultBuilder.success(handler.truckQuery(config, dto));
    }

    @Override
    public ResultJson<List<String>> customerQuery(Integer projectId, String requestTime, String transportOrderCode) {
        VehicleScheduleIntegrationConfig config = onFindingConfig(projectId);
        VehicleScheduleSystemHandler handler = vehicleScheduleFactory.onFindingIntegrationHandler(config.getProductName());
        return ResultBuilder.success(handler.customerQuery(config, projectId, requestTime, transportOrderCode));
    }

    @Override
    public ResultJson getSpaceCodeInfo(VehicleScheduleRequestsDTO dto) {
        VehicleScheduleIntegrationConfig config = onFindingConfig(dto.getProjectId());
        vehicleScheduleFactory.onFindingIntegrationHandler(config.getProductName());
        ResultJson list = spaceCentService.list(new SpaceQuery());
        List<Object> collect = JSONObject.parseArray(JSONObject.parseObject(JSONObject.toJSONString(list)).getString("result"))
                .stream().map(o -> JSONObject.parseObject(JSONObject.parseObject(o.toString()).getString("data"))).collect(Collectors.toList());
        return ResultJson.success(collect);
    }
}
